package net.osc.web.query;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.osc.model.query.QuerySegment;
import net.osc.model.query.QueryUnit;
import net.osc.service.query.CommonQueryManager;
import net.osc.util.PageBean;
import net.osc.web.BaseController;
import net.osc.web.JsonResult;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CommonQueryController extends BaseController {

	@Resource
	protected CommonQueryManager commonQueryManager;

	public CommonQueryManager getCommonQueryManager() {
		return commonQueryManager;
	}

	public void setCommonQueryManager(CommonQueryManager commonQueryManager) {
		this.commonQueryManager = commonQueryManager;
	}

	@RequestMapping("/**/query/queryByKey.do")
	@ResponseBody
	public Object queryByKey(HttpServletRequest req, String queryKey) {
		return commonQueryManager.doQuery(queryKey, getParamMapFromRequest(req));
	}
	
	@RequestMapping("/**/query/query.do")
	@ResponseBody
	public Object query(HttpServletRequest req, QueryUnit queryUnit) {
		return commonQueryManager.doQuery(queryUnit, getParamMapFromRequest(req));
	}

	@RequestMapping("/**/query/pageQueryByKey.do")
	@ResponseBody
	public Object pageQueryByKey(HttpServletRequest req, String queryKey, PageBean<?> pageBean) {
		return commonQueryManager.doPageQuery(queryKey, getParamMapFromRequest(req), pageBean);
	}
	
	@RequestMapping("/**/query/pageQuery.do")
	@ResponseBody
	public Object pageQuery(HttpServletRequest req, QueryUnit queryUnit, PageBean<?> pageBean) {
		return commonQueryManager.doPageQuery(queryUnit, getParamMapFromRequest(req), pageBean);
	}

	@RequestMapping("/**/query/pageQuery4Easyui.do")
	@ResponseBody
	public Object pageQuery4Easyui(HttpServletRequest req, int page, int rows, String queryKey,
			QueryUnit queryUnit) {
		Map<String, Object> result = new HashMap<String, Object>();
		PageBean<?> pageBean = new PageBean<Object>(page, rows);
		this.pageQueryByKey(req, queryKey, pageBean);
		result.put("total", pageBean.getCount());
		result.put("rows", pageBean.getData());
		return result;
	}

	@RequestMapping("/**/query/delQueryUnits.do")
	@ResponseBody
	public Object delQueryUnits(Long[] ids) {
		commonQueryManager.delQueryUnits(ids);
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/query/delQuerySegments.do")
	@ResponseBody
	public Object delQuerySegments(Long[] ids) {
		commonQueryManager.delQuerySegments(ids);
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/query/saveQueryUnit.do")
	@ResponseBody
	public Object saveQueryUnit(QueryUnit queryUnit) {
		commonQueryManager.saveQueryUnit(queryUnit);
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/query/saveQuerySegment.do")
	@ResponseBody
	public Object saveQuerySegment(QuerySegment querySegment) {
		commonQueryManager.saveQuerySegment(querySegment);
		return new JsonResult(true, "");
	}

	@RequestMapping("/**/query/moveQuerySegment.do")
	@ResponseBody
	public Object moveQuerySegment(Long id, int index) {
		QuerySegment qs = hibernateDao.get(QuerySegment.class, id);
		qs.setSegmentOrder(index);
		commonQueryManager.saveQuerySegment(qs);
		return new JsonResult(true, "");
	}

	/**
	 * 从请求中收集参数,包括session中的参数
	 * 
	 * @param req
	 * @return
	 */
	public static Map<String, Object> getParamMapFromRequest(HttpServletRequest req) {
		Map<String, Object> params = new HashMap<String, Object>();
		Enumeration<String> sessionAttrNames = req.getSession().getAttributeNames();
		while (sessionAttrNames.hasMoreElements()) {
			String sessionAttrName = sessionAttrNames.nextElement();
			params.put(sessionAttrName, req.getSession().getAttribute(sessionAttrName));
		}
		Enumeration<String> paramNames = req.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = paramNames.nextElement();
			params.put(paramName, req.getParameter(paramName));
		}
		return params;
	}

}
